package cn.dengqiang.product.controller;

import cn.dengqiang.basic.util.AjaxResult;
import cn.dengqiang.basic.util.PageList;

import cn.dengqiang.product.domain.Product;
import cn.dengqiang.product.domain.ProductDetail;
import cn.dengqiang.product.query.ProductQuery;
import cn.dengqiang.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController   //=@ResponseBody+@Controller
@RequestMapping("/product")

@Api(tags = "服务接口",description = "服务接口详细描述")
public class ProductController {

    @Autowired
    private IProductService productService;

    @PutMapping
    public AjaxResult save(@RequestBody Product product){
        try {
            if (product.getId()==null){
                productService.add(product);
            }else {
                productService.update(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存失败"+e.getMessage());
        }
    }




    @ApiOperation(value = "删除",notes = "根据id删除一条数据")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            productService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
           return new AjaxResult("删除失败!"+e.getMessage());
       }
    }

    @ApiOperation(value = "查询",notes = "根据id查询一条数据")
    @GetMapping("/{id}") //根据id查询
    public Product getById(@PathVariable("id") Long id){
        return productService.getById(id);
    }

    @ApiOperation(value = "查询",notes = "查询所有信息")
    @GetMapping //product 查询所有
    public List<Product> getAll(){
        return productService.getAll();
    }

    @ApiOperation(value = "查询",notes = "分页查询")
    @PostMapping("/list")//分页查询
    public PageList<Product> query(@RequestBody ProductQuery query){
        System.out.println("我进来了");
        query.setState(1);
        return productService.queryPage(query);
    }
    @ApiOperation(value = "查询",notes = "分页查询")
    @PostMapping//分页查询
    public PageList<Product> list(@RequestBody ProductQuery query){
        System.out.println("我进来了");
        return productService.queryPage(query);
    }

    //获取一个 get
    @GetMapping("/detail/{productId}") //Get http://localhost:8080/deparment/1 deparment/2
    public ProductDetail getDetailByProductId(@PathVariable("productId") Long productId){
        return productService.getDetailByProductId(productId);
    }

    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            productService.onOrOffsale(ids,1);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("上架失败"+e.getMessage());
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            productService.onOrOffsale(ids,0);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("上架失败"+e.getMessage());
        }
    }
    
}
